import sys
from PySide6.QtWidgets import (QApplication, QMainWindow, QLabel, 
                               QVBoxLayout, QWidget, QHBoxLayout)
from PySide6.QtGui import QPixmap
from PySide6.QtCore import Qt, QPoint, QSize

class ImagePreviewLabel(QLabel):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setFixedSize(200, 200)
        self.setAlignment(Qt.AlignCenter)
        self.setStyleSheet("""
            border: 1px solid gray;
            background-color: #f0f0f0;
        """)
        
        # 创建放大预览标签
        self.preview_label = QLabel()
        self.preview_label.setWindowFlags(Qt.ToolTip | Qt.FramelessWindowHint)
        self.preview_label.setStyleSheet("""
            border: 2px solid #cccccc;
            background-color: white;
        """)
        self.preview_label.setAlignment(Qt.AlignCenter)
        self.preview_label.hide()
        
        # 原图路径
        self.original_image_path = ""
        
    def set_image(self, image_path):
        """设置图片路径并加载图片"""
        self.original_image_path = image_path
        
        # 加载缩略图 - 使用标签大小
        pixmap = QPixmap(image_path)
        if not pixmap.isNull():
            # 保持宽高比缩放图片
            scaled_pixmap = pixmap.scaled(
                self.size() - QSize(10, 10),  # 稍微小于标签大小，留出边距
                Qt.KeepAspectRatio, 
                Qt.SmoothTransformation
            )
            self.setPixmap(scaled_pixmap)
        else:
            self.setText("图片加载失败")
        
    def enterEvent(self, event):
        """鼠标进入事件 - 显示放大预览"""
        if self.original_image_path:
            # 加载原图
            original_pixmap = QPixmap(self.original_image_path)
            
            if not original_pixmap.isNull():
                # 获取屏幕尺寸，限制预览图的最大尺寸
                screen_geometry = QApplication.primaryScreen().availableGeometry()
                max_width = screen_geometry.width() * 0.6
                max_height = screen_geometry.height() * 0.6
                
                # 缩放图片以适应屏幕
                if (original_pixmap.width() > max_width or 
                    original_pixmap.height() > max_height):
                    original_pixmap = original_pixmap.scaled(
                        int(max_width), int(max_height), 
                        Qt.KeepAspectRatio, 
                        Qt.SmoothTransformation
                    )
                
                self.preview_label.setPixmap(original_pixmap)
                self.preview_label.adjustSize()
                
                # 计算预览窗口位置（确保不会超出屏幕）
                label_pos = self.mapToGlobal(QPoint(0, 0))
                preview_size = self.preview_label.size()
                
                # 尝试在右侧显示
                preview_x = label_pos.x() + self.width() + 5
                preview_y = label_pos.y()
                
                # 如果右侧空间不够，尝试在左侧显示
                if preview_x + preview_size.width() > screen_geometry.right():
                    preview_x = label_pos.x() - preview_size.width() - 5
                
                # 如果上下空间不够，调整Y位置
                if preview_y + preview_size.height() > screen_geometry.bottom():
                    preview_y = screen_geometry.bottom() - preview_size.height() - 5
                
                self.preview_label.move(preview_x, preview_y)
                self.preview_label.show()
        
        super().enterEvent(event)
        
    def leaveEvent(self, event):
        """鼠标离开事件 - 隐藏放大预览"""
        self.preview_label.hide()
        super().leaveEvent(event)
        
    def mousePressEvent(self, event):
        """点击事件 - 隐藏预览"""
        self.preview_label.hide()
        super().mousePressEvent(event)



if __name__ == "__main__":
    
    class MainWindow(QMainWindow):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("图片放大预览示例")
            self.setGeometry(100, 100, 800, 400)
            
            # 创建中央部件和布局
            central_widget = QWidget()
            self.setCentralWidget(central_widget)
            
            
            # layout = QHBoxLayout(central_widget)
            # layout.setSpacing(20)
            # layout.setContentsMargins(20, 20, 20, 20)
            
            # 创建几个示例图片标签
            image_paths = [
                "123.jpg",  # 替换为实际图片路径
                "123.jpg",  # 替换为实际图片路径
                "123.jpg"   # 替换为实际图片路径
            ]
            
            # for path in image_paths:
            #     image_label = ImagePreviewLabel()
            #     image_label.set_image(path)
            #     layout.addWidget(image_label)

            
            i1 = ImagePreviewLabel(self)
            i1.set_image("123.jpg")
            i1.move(50,50)
            
            i2 = ImagePreviewLabel(self)
            i2.set_image("123.jpg")
            i2.move(250,50)
        

    app = QApplication(sys.argv)
    
    window = MainWindow()
    window.show()
    
    sys.exit(app.exec())